package funcs

import "fmt"

/**
演示不定参数的用法。
**/
func sum(nums ...int) {
	fmt.Print(nums, " ")
	total := 0
	for _, num := range nums {
		total += num
	}

	fmt.Println(total)
}

/**
演示用任意类型不定参数。
用interface{}传递任意类型数据是Go语言的惯例用法
**/
func MyPrintf(args ...interface{}) {
	for _, arg := range args {
		switch arg.(type) {
		case int:
			fmt.Println(arg, "is an int value.")
		case string:
			fmt.Println(arg, "is a string.")
		case int64:
			fmt.Println(arg, "is an int64 value.")
		default:
			fmt.Println(arg, "is unknown type.")
		}
	}
}

func VariadicFunctions() {
	sum(1, 2)
	sum(1, 2, 3)

	nums := []int{1, 2, 3, 4} //数组切片
	sum(nums...)
}
